/*---------------------------------------------------------------------------
 * Speculoos, LDAP to objet mapping.
 * Copyright (C) 2006  Norsys and OQube
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * --------------------------------------------------------------------------*/
package speculoos.commons.mockldap;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;

class MatchFilter extends Filter {

    private String name;
    private Object value;
    MatchFilter(Attribute attr) throws NamingException {
        this.name = attr.getID();
        // TODO: handle multiple values
        this.value = attr.get();
    }

    public boolean match(TestDirContext ctx) throws NamingException {
        Attribute attr = ctx.getAttributes("",new String[]{name}).get(name);
        if(attr == null)
            return false;
        /* check object */
        for(NamingEnumeration e = attr.getAll();e.hasMore();) {
        	Object val = e.next();
        	if (val instanceof String) {
        		if (((String)val).equalsIgnoreCase((String) value)) {
        			return true;
        		}
			} else if(val.equals(value))
                return true;
        }
        return false;
    }

}
